aboutsummaryrefslogtreecommitdiff
path: root/pages/blog/[post].tsx
blob: fe25007e9f13bcfe53d39a00c857470191992f4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { readdirSync, readFileSync } from 'fs';
import { join } from 'path';

import ReactMarkdown from 'react-markdown';
import rehypeRaw from 'rehype-raw';
import gfm from 'remark-gfm';

import { Footer } from '../../components/footer';
import { NavBar } from '../../components/navbar';
import { CenteredPage, PageTitle } from '../../components/page';
import { Vierkant } from '../../components/ui';

export interface ArticleMeta {
	title?: string;
	id?: string;
}

export function RenderedArticle(props: {
	content: string;
	meta: ArticleMeta;
	standalone?: boolean;
}) {
	return <Vierkant className='pad-l bg-800 w100m2m postContent'>
		<ReactMarkdown
			rehypePlugins={[rehypeRaw]}
			remarkPlugins={[gfm]}
			children={(props.standalone ? '' : '## ' + props.meta.title + '\n\n') + props.content}
		/>
	</Vierkant>;
}

export default function Post(props: {
	content: string;
	meta: ArticleMeta;
}) {
	return <div>
		<NavBar />
		<CenteredPage width={802} className='blogPost'>
			<PageTitle>{props.meta.title}</PageTitle>
			<RenderedArticle content={props.content} meta={props.meta} standalone />
		</CenteredPage>
		<Footer />
	</div>;
}

var parseTag = {
	'title': (val: string) => val,
};

function parseMeta(file: Array<string>): ArticleMeta {
	var meta: ArticleMeta = {};

	file.forEach(line => {
		if (!line.startsWith('[meta]: ')) return;
		var tags = line.match(/\[meta\]:\s+\<(.+?)\>\s+\((.+?)\)/);
		if (!tags || !tags[1] || !tags[2]) return;
		if (!parseTag.hasOwnProperty(tags[1])) return;
		meta[tags[1]] = parseTag[tags[1]](tags[2]);
	});

	return meta;
}

function preprocessor(fileContent: string) {
	var fileAsArr = fileContent.split('\n');
	var meta = parseMeta(fileAsArr);
	var result = fileAsArr.join('\n').trim();
	return { meta, result };
}

export function getStaticProps(props: { params: { post: string; }; }) {
	var filename = join('news/', props.params.post + '.md');
	var filecontent = readFileSync(filename).toString().trim();

	var parsed = preprocessor(filecontent);
	parsed.meta.id = props.params.post;

	return {
		props: {
			content: parsed.result,
			meta: parsed.meta,
		},
	};
}

export function getStaticPaths() {
	var files = readdirSync('news').filter(f => f.endsWith('.md'));

	return {
		paths: files.map((f) => {
			return {
				params: {
					post: f.substr(0, f.length - 3),
				},
			};
		}),
		fallback: false,
	};
}